package com.brillilab.service.core.instrument.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brillilab.dao.mapper.instrument.InstrumentMapper;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.instrument.Instrument;
import com.brillilab.service.core.instrument.IInstrumentService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 仪器 服务实现类
 * </p>
 *
 * @author WuMenghao
 * @since 2019-06-12
 */
@Service
@Transactional
public class InstrumentServiceImpl implements IInstrumentService {

    @Resource
    private InstrumentMapper instrumentMapper;

    @Override
    public List<Long> selectDuties(Long labId) {
        List<Instrument> instruments=instrumentMapper.selectList(
                new LambdaQueryWrapper<Instrument>()
                        .eq(Instrument::getLabId,labId)
                        .eq(Instrument::getIsDelete,BoolEnum.FALSE.getValue()));
        List<Long> dutyIds=instruments.stream().map(Instrument::getDutyId).distinct().collect(Collectors.toList());
        return dutyIds;
    }

    @SuppressWarnings("unchecked")
    @Override
    public IPage<Instrument> selectPage(Page<Instrument> page,LambdaQueryWrapper<Instrument> wrapper) {
        wrapper.orderByAsc(Instrument::getNo);
        return instrumentMapper.selectPage(page,wrapper);
    }

    @Override
    public int selectCount(LambdaQueryWrapper<Instrument> wrapper) {
        return instrumentMapper.selectCount(wrapper);
    }

    @Override
    public Instrument selectOne(Long labId,Integer no) {
        return instrumentMapper.selectOne(
                new LambdaQueryWrapper<Instrument>()
                        .eq(Instrument::getLabId,labId)
                        .eq(Instrument::getNo,no)
                        .eq(Instrument::getIsDelete,BoolEnum.FALSE.getValue()));
    }

    @Override
    public int insert(Instrument instrument) {
        return instrumentMapper.insert(instrument);
    }

    @Override
    public Instrument selectById(Long instrumentId) {
        return instrumentMapper.selectById(instrumentId);
    }

    @Override
    public int updateById(Instrument instrument) {
        instrument.setUpdateTime(new Date());
        return instrumentMapper.updateById(instrument);
    }

    @Override
    public List<Instrument> selectList(LambdaQueryWrapper<Instrument> wrapper) {
        return instrumentMapper.selectList(wrapper);
    }

    @Override
    public Integer[] getUsedInstrumentNo(Long labId) {
        return instrumentMapper.getUsedInstrumentNo(labId);
    }

    @Override
    public List<Instrument> selectList(Long labId,Integer isDelete) {
        return instrumentMapper.selectList(
                new LambdaQueryWrapper<Instrument>()
                        .eq(Instrument::getLabId,labId)
                        .eq(Instrument::getIsDelete,isDelete));
    }

    @Override
    public List<Instrument> selectList(List<Long> instrumentIds) {
        if(CollectionUtils.isNotEmpty(instrumentIds)){
            return instrumentMapper.selectList(
                    new LambdaQueryWrapper<Instrument>()
                            .in(Instrument::getDutyName,instrumentIds));
        }
        return new ArrayList<>(0);
    }
}
